<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace app\adminapi\services\setting;

use app\dao\setting\AttachmentCateDao;
use kitego\base\BaseService;

class AttachmentCateService extends BaseService
{
    public function __construct(AttachmentCateDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 附件分类列表
     */
    public function attachmentCategoryList(int $type)
    {
        $cateList = $this->dao->select([['type', '=', $type], ['is_delete', '=', 0]])->toArray();

        return success(make_tree($cateList));
    }

    /**
     * 附件分类新增
     */
    public function attachmentCategoryAdd($name, $pid, $type)
    {
        $data = [
            'name' => $name,
            'pid' => $pid,
            'type' => $type
        ];

        return success($this->dao->save($data));
    }

    /**
     * 附件分类重命名
     */
    public function attachmentCategoryRename($id, $name)
    {
        // 命名是否重复
        $existQuery = $this->dao->find([['name', '=', $name]]);
        if ($existQuery) exception('分类名称已存在');

        return success($this->dao->update($id, ['name' => $name]));
    }

    /**
     * 附件分类删除
     */
    public function attachmentCategoryDelete($id)
    {
        return success($this->dao->update($id, ['is_delete' => 1]));
    }
}